package com.example.springboot.controller;

import com.example.springboot.common.Result;
import com.example.springboot.controller.dto.LoginDto;
import com.example.springboot.controller.request.AdminPageRequest;
import com.example.springboot.controller.request.LoginRequest;
import com.example.springboot.controller.request.PasswordRequest;
import com.example.springboot.entity.Admin;
import com.example.springboot.service.IAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Description:Admin控制层，接收和请求数据
 *
 * @Author 马钰涵
 * @Create 2023/8/4 23:39
 * @Version 1.0
 */
//@CrossOrigin                        //处理跨域方式之一
@RestController                     //指明这是一个接口，所有数据都是Json格式的
@RequestMapping("/admin")         //后端父级路由
public class AdminController {

    @Autowired
    private IAdminService adminService;

    /**
     * 登录
     */
    @PostMapping("/login")
    public Result login(@RequestBody LoginRequest request) {
        LoginDto login = adminService.login(request);
        return Result.success(login);
    }

    /**
     * 修改信息
     */
    @PutMapping("/update")
    public Result update(@RequestBody Admin admin) {
        adminService.update(admin);
        return Result.success();
    }
    /**
     * 修改密码
     */
    @PutMapping("/updatePassword")
    public Result updatePassword(@RequestBody PasswordRequest request) {
        adminService.updatePassword(request);
        return Result.success();
    }

    /**
     * 根据id获取数据
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        Admin admin = adminService.getById(id);
        return Result.success(admin);
    }

    /**
     * 根据id删除数据
     */
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Integer id) {
        adminService.deleteById(id);
        return Result.success();
    }

    /**
     * 新增接口
     */
    @PostMapping("/save")
    public Result save(@RequestBody Admin admin) {
        adminService.save(admin);
        return Result.success();
    }


    /**
     * 查询所有数据
     */
    @GetMapping("/list")
    public Result list() {
        return Result.success(adminService.list());
    }

    /**
     * 分页模糊查询
     */
    @GetMapping("/page")
    public Result page(AdminPageRequest adminPageRequest) {
        return Result.success(adminService.page(adminPageRequest));
    }
}
